<?php

/**
 * worry actions.
 *
 * @package    jiazhang
 * @subpackage worry
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class worryActions extends sfActions
{

   public function executeCreate_comment(sfWebRequest $request){
      $msg = array('error'=>'','msg'=>'');
      if($request->isMethod('POST')){
          $user_id = (int)$request->getParameter('person_id',0);
          $worry_id = (int)$request->getParameter('worry_id',0);
          $content = trim($request->getParameter('content',0));
         $comment = new WorryComment();
         $comment->setUserId($user_id)->setWorryId($worry_id)->setContent($content)->save();
         $comment->getWorry()->addCommentNum();
         $msg['error'] = 0;
         $msg['msg'] = '评论发布成功！';
      }
      
      return $this->renderText(json_encode($msg));
   }
    
    
  public function executeCreate_answer(sfWebRequest $request)
  {
      $msg = array('error'=>'','msg'=>'');
      if($request->isMethod('POST')){
          $user_id = (int)$request->getParameter('person_id',0);
          $worry_id = (int)$request->getParameter('worry_id',0);
          $answer_id = (int)$request->getParameter('answer_id',0);
          $content = trim($request->getParameter('content',0));
          
          $worry_answer = new WorryAnswer();
          $worry_answer->setUserId($user_id)->setContent($content)->setWorryId($worry_id)->setParentId($answer_id)->save();
          $msg['error'] = 0;
          
          if($answer_id == 0){
            $msg['msg'] = '解答发布成功！';
          }else{
            $msg['msg'] = '回复发布成功！';
          }
         
      }
     return $this->renderText(json_encode($msg));
  }
    
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeShow(sfWebRequest $request)
  {
    $worry_id = (int)$request->getParameter('worry_id',0);
    $worry = Doctrine::getTable('Worry')->findOneById($worry_id);
    $worry_data = array();
    
    if($worry){
        $worry_data['id'] = $worry->getId();
        $worry_data['titile'] = $worry->getTitle();
        $worry_data['detail'] = $worry->getContent();
        $worry_data['createTime'] = date("Y-m-d H:i:s",strtotime($worry->getCreatedAt()));
        $worry_data['person'] = array();
        $worry_data['person']['id'] = $worry->getUser()->getId();
        $worry_data['person']['name'] = $worry->getAuthorName();
        $worry_data['person']['icon'] = $worry->getAuthorCover();
        $worry_data['person']['city'] = $worry->getAuthorCity();
        $worry_data['person']['children'] = array();

        foreach($worry->getAuthorChildrens() as $i => $child){
            $worry_data['person']['children'][$i]['age'] = $child->getChildOld();
            $worry_data['person']['children'][$i]['sex'] = $child->getSexToString();
        }
        
        $worry_data['answerList'] = array();
        $worry_answer = Doctrine::getTable('WorryAnswer')->createQuery()->where('worry_id = ?',$worry_id)->andWhere('parent_id = 0')->orderBy('created_at asc')->execute();
        
        foreach( $worry_answer as $key=>$answer){
            $worry_data['answerList'][$key]['id'] = $answer->getId();
            $worry_data['answerList'][$key]['detail'] = $answer->getContent();
            $worry_data['answerList'][$key]['commendation_num'] = $answer->getUser()->getUserAttr()->getCommendationNum();
            $worry_data['answerList'][$key]['person'] = array();
            $worry_data['answerList'][$key]['person']['id'] = $answer->getUser()->getId();
            $worry_data['answerList'][$key]['person']['name'] = $answer->getUser()->getNickname();
            $worry_data['answerList'][$key]['person']['icon'] = $answer->getUser()->getCover();
            $worry_data['answerList'][$key]['person']['city'] = $answer->getUser()->getCity();
            $worry_data['answerList'][$key]['person']['familyRole'] = User::$roles[$answer->getUser()->getRole()];
            $worry_data['answerList'][$key]['person']['children'] = array();
            
            foreach($answer->getUser()->getChilds() as $i => $child){
                $worry_data['answerList'][$key]['person']['children'][$i]['age'] = $child->getChildOld();
                $worry_data['answerList'][$key]['person']['children'][$i]['sex'] = $child->getSexToString();
            }
            
            $worry_data['answerList'][$key]['replyList'] = array();
            
            $worry_answer_reply = Doctrine::getTable('WorryAnswer')->createQuery()->where('parent_id = ?',$answer->getId())->andWhere('display = 1')->orderBy('created_at asc')->execute();
            
            foreach( $worry_answer_reply as $r_key => $reply){
                $worry_data['answerList'][$key]['replyList'][$r_key]['person_id'] = $reply->getUser()->getId();
                $worry_data['answerList'][$key]['replyList'][$r_key]['person_icon'] = $reply->getUser()->getCover();
                $worry_data['answerList'][$key]['replyList'][$r_key]['detail'] = $reply->getContent();
                $worry_data['answerList'][$key]['replyList'][$r_key]['id'] = $reply->getId();
            }
        }
        
        $worry_data['commentList'] = array();
        $worry_comment = Doctrine::getTable('WorryComment')->createQuery()->where('worry_id = ?',$worry_id)->andWhere('display = 1')->orderBy('created_at asc')->execute();
        
        foreach( $worry_comment as $comment_key => $comment){
            $worry_data['commentList'][$comment_key]['id'] = $comment->getId();
                    $worry_data['commentList'][$comment_key]['content'] = $comment->getContent();
                    $worry_data['commentList'][$comment_key]['person'] = array();
                    $worry_data['commentList'][$comment_key]['person']['id'] = $comment->getUser()->getId();
                    $worry_data['commentList'][$comment_key]['person']['name'] = $comment->getUser()->getNickname();
                    $worry_data['commentList'][$comment_key]['person']['icon'] = $comment->getUser()->getCover();
                    $worry_data['commentList'][$comment_key]['person']['family'] = User::$roles[$comment->getUser()->getRole()];
                    $worry_data['commentList'][$comment_key]['person']['children'] = array();
                    
                    foreach($comment->getUser()->getChilds() as $i => $child){
                        $worry_data['commentList'][$comment_key]['person']['children'][$i]['age'] = $child->getChildOld();
                        $worry_data['commentList'][$comment_key]['person']['children'][$i]['sex'] = $child->getSexToString();
                    }
        }

    }
    
   return $this->renderText(json_encode($worry_data));
  }

  /**
   * 
   * @param sfWebRequest $request
   * 烦恼类型。
    0：所有烦恼
    1、热门解答
    2、我的烦恼
    3、我的解答
   */
  public function executeList(sfWebRequest $request)
  {
      $type = (int)$request->getParameter('type',0);
      $page = (int)$request->getParameter('page',0);
      $page_size = (int)$request->getParameter('page_size',5);
      $user_id = (int)$request->getParameter('person_id',0);
      
      if($type == 0){
          $dql = Doctrine::getTable('Worry')->createQuery()->andWhere('display = 1')->orderBy('created_at desc');
      }elseif($type == 1){
          $worry_ids = Doctrine::getTable('WorryStatistical')->createQuery()->orderBy('attention_num desc')->execute()->toKeyValueArray('worry_id', 'worry_id');
          $dql = Doctrine::getTable('Worry')->createQuery()->whereIn('id',$worry_ids)->andWhere('display = 1')->orderBy('created_at desc');
    }elseif($type == 2){
          $dql = Doctrine::getTable('Worry')->createQuery()->where('user_id = ?',$user_id)->andWhere('display = 1')->orderBy('created_at desc');
    }elseif($type == 3){
        $worry_ids = Doctrine::getTable('WorryAnswer')->createQuery()->where('user_id = ?',$user_id)->andWhere('parent_id = 0')->execute()->toKeyValueArray('worry_id', 'worry_id');
        if(count($worry_ids) != 0){
            $dql = Doctrine::getTable('Worry')->createQuery()->whereIn('id',$worry_ids)->andWhere('display = 1');
        }else{
            $dql = Doctrine::getTable('Worry')->createQuery()->where('id = 0');
        }
    }
    
        $this->pager = new sfDoctrinePager('Worry',$page_size);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
        $worry_data = array();
        
        foreach($this->pager->getResults() as $key=>$worry){
            $worry_data[$key]['id'] = $worry->getId();
            $worry_data[$key]['titile'] = $worry->getTitle();
            $worry_data[$key]['detail'] = $worry->getContent();
            $worry_data[$key]['createTime'] = date("Y-m-d H:i:s",strtotime($worry->getCreatedAt()));
            $worry_data[$key]['comments'] = $worry->getCommentNum();
            $worry_data[$key]['answers'] = $worry->getAnswerNum();
            $worry_data[$key]['person'] = array();
            $worry_data[$key]['person']['id'] = $worry->getUser()->getId();
            $worry_data[$key]['person']['name'] = $worry->getAuthorName();
            $worry_data[$key]['person']['icon'] = $worry->getAuthorCover();
            $worry_data[$key]['person']['city'] = $worry->getAuthorCity();
            $worry_data[$key]['person']['children'] = array();

            foreach($worry->getUser()->getChilds() as $i => $child){
                
                $worry_data[$key]['person']['children'][$i]['age'] = $child->getChildOld();
                $worry_data[$key]['person']['children'][$i]['sex'] = $child->getSexToString();
            }
        }
        
        return $this->renderText(json_encode($worry_data));
  }
  
  public function executeCommendation(sfWebRequest $request){
      if($request->isMethod('POST')){
          $user_id = (int)$request->getParameter('action_user_id',0);
          $answer_id = (int)$request->getParameter('answer_id',0);
          $answer_user_id = (int)$request->getParameter('person_id',0);
          $msg = array('error'=>'','msg'=>'');
          $has_commendation = Doctrine::getTable('WorryCommendation')->createQuery()->where('answer_id = ?',$answer_id)->andWhere('action_user_id  = ?',$user_id)->execute();
          
          if(count($has_commendation) > 0){
              $msg['error'] = 1;
              $msg['msg'] = '你已经打赏过了哦';
              return $this->renderText(json_encode($msg));
          }else{
              $commendation = new WorryCommendation();
              $commendation->setAnswerId($answer_id)->setUserId($answer_user_id)->setActionUserId($user_id)->save(); 
               $num = Doctrine::getTable('WorryAnswer')->addCommendationNum($answer_id);
               $msg['error'] = 0;
               $msg['msg'] = '打赏成功，谢谢你的关注';
               return $this->renderText(json_encode($msg));
          }
      }
  }
  
  public function executeCreate(sfWebRequest $request){
      if($request->isMethod('POST')){
          $title = trim($request->getParameter('title',''));
          $content = trim($request->getParameter('content',''));
          $user_id = (int)$request->getParameter('person_id',0);
          $current_user = Doctrine::getTable('User')->findOneById($user_id);
          $msg = array('error'=>'','msg'=>'');
          
          if(strlen($title) == 0 || strlen($content) == 0){
              $msg['error'] = 1;
              $msg['msg'] = '标题和内容不能为空';
              return $this->renderText(json_encode($msg));
          }
          
          if(!$current_user){
              $msg['error'] = 1;
              $msg['msg'] = '用户不存在，请联系管理员';
              return $this->renderText(json_encode($msg));
          }
          
         $worry_obj = new Worry();
         $worry_obj->setUserId($user_id)->setTitle($title)->setContent($content)->save();
          $msg['error'] = 0;
          $msg['msg'] = '数据保存成功！';
          return $this->renderText(json_encode($msg));
      }
  }
  
  
}
